package it.polimi.traveldream.web.beans.pacchetti;

import it.polimi.traveldream.persistence.dtos.IDable;

import java.util.Collection;
import java.util.Iterator;

public class PacchettiUtils {


	public static <T extends IDable> boolean checkIDPresence(Collection<T> idAbles, int ID) {
		for(T idable : idAbles) {
			if(idable.getID() == ID)
				return true;
		}
		return false;
	}

	public static <T extends IDable> T findByID(Collection<T> idAbles, int ID) {
		for(T idable : idAbles) {
			if(idable.getID() == ID)
				return idable;
		}
		return null;
	}

	public static <T extends IDable> void removeElementWithID(Collection<T> idAbles, Collection<Integer> IDs) {
		Iterator<T> iter = idAbles.iterator();
		while(iter.hasNext()) {
			T element = iter.next();
			if(IDs.contains(element.getID()))
				iter.remove();
		}
	}
	
	public static <T extends IDable, K extends IDable> void removeElementWithIDable(Collection<T> idAbles, Collection<K> IDs) {
		Iterator<T> iter = idAbles.iterator();
		while(iter.hasNext()) {
			T element = iter.next();
			Iterator<K> ids = IDs.iterator();
			while(ids.hasNext()) {
				if(element.getID() == ids.next().getID()) {
					iter.remove();
					break;
				}
			}
		}
	}
}
